package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.dto.StoreOperDto;
import com.codingfuture.erp_system.entity.Goods;
import com.codingfuture.erp_system.entity.Store;
import com.codingfuture.erp_system.service.StoreOperService;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/v2")
@RestController
public class StoreOperController {

    @Autowired
    private StoreOperService storeOperService;

    @GetMapping("")
    public Result findById(Integer uuid, HttpSession session) {
        session.setAttribute("uuid", uuid);
        StoreOperDto storeOper = storeOperService.findById(uuid);
        return Result.OK(storeOper);
    }

    @GetMapping("/storedetail")
    public Result findByAll(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int rows,
                            Integer storeName, Integer goodsName) {
        Page<StoreOperDto> page1 = PageHelper.startPage(page, rows);
        List<StoreOperDto> List = storeOperService.findByAll(storeName, goodsName);
        PageData<StoreOperDto> data = new PageData<>(List, page1.getTotal());
        return Result.ok(data);
    }

    @GetMapping("/storedetail/goodsList")
    public Result findAll() {
        List<Goods> list = storeOperService.findAll();
        return Result.OK(list);
    }

    @GetMapping("/storedetail/storeList")
    public Result finAll() {
        List<Store> list = storeOperService.finAll();
        return Result.OK(list);
    }
}
